import { alova } from '../../request';

/** get role list */
export function paginateRole(params?: Api.SearchParams) {
  return alova.Get<Api.Manage.RoleList>('/permission/role/paginate', { params });
}

export function addRole(data?: Api.Model) {
  return alova.Post<boolean>('/permission/role/create', data);
}

export function updateRole(data?: Api.Model) {
  return alova.Post<boolean>('/permission/role/update', data);
}

export function deleteRole(id: number) {
  return alova.Post<null>('/permission/role/delete', { id });
}

export function batchDeleteRole(id: number[]) {
  return alova.Post<null>('/permission/role/delete', { id });
}
/**
 * get all roles
 *
 * these roles are all enabled
 */
export function listRole(status?: number) {
  return alova.Get<Api.Manage.AllRole[]>('/permission/role/list', { params: { status } });
}

// 更新角色权限
export function updateRolePermission(data?: Api.Manage.RolePermission) {
  return alova.Post<boolean>('/permission/rolePermission/update', data);
}

// 获取角色权限
export function fetchRolePermission(id: number) {
  return alova.Get<Api.Manage.RolePermission>('/permission/rolePermission/list', {
    params: { roleId: id },
    cacheFor: null
  });
}

// 更新角色用户
export function updateRoleUser(data?: Api.Manage.RoleUser) {
  return alova.Post<boolean>('/permission/accountRole/update', data);
}

export function addRoleUser(roleId: number, accountId: number) {
  return alova.Post<boolean>('/permission/accountRole/add', {
    roleId,
    accountId
  });
}

// 获取角色用户
export function fetchRoleUser(id: number) {
  return alova.Get<Api.Manage.UserList>('/permission/accountRole/list', { params: { roleId: id } });
}

// 删除角色用户
export function deleteByUser(userId: number) {
  return alova.Post<boolean>('/permission/accountRole/deleteByUser', { userId });
}

export function deleteByUsers(userIds: number[]) {
  return alova.Post<boolean>('/permission/accountRole/deleteByUsers', { userIds });
}

// 删除角色用户
export function deleteByRole(roleId: number) {
  return alova.Post<boolean>('/permission/accountRole/deleteByRole', { roleId });
}

/** get user paginate */
export function paginateRoleUser(params?: Api.Manage.RoleUserSearchParams) {
  return alova.Get<Api.Manage.UserList>('/permission/accountRole/paginate', { params, cacheFor: null });
}
